package com.ruoyi.config.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.config.manager.mapper.MgIdentityMapper;
import com.basic.service.impl.BaseServiceImpl;
import com.ruoyi.config.manager.domain.MgIdentity;
import com.ruoyi.config.manager.service.IMgIdentityService;
import com.ruoyi.constant.RedisConstant;
import com.translate.mapper.IBaseMapper;

/**
 * 身份Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-20
 */
@Service
public class MgIdentityServiceImpl extends BaseServiceImpl<Long, MgIdentity> implements IMgIdentityService {
	public MgIdentityServiceImpl(MgIdentityMapper mgIdentityMapper) {
		super(mgIdentityMapper);
	}

	private RedisTemplate<String, List<MgIdentity>> redisTemplate;

	/**
	 * 查询身份
	 * 
	 * @param id 身份主键
	 * @return 身份
	 */
	@Override
	public MgIdentity selectMgIdentityById(int id) {
		return this.getBaseMapper().selectById(id);
	}

	/**
	 * 查询身份列表
	 * 
	 * @param mgIdentity 身份
	 * @return 身份
	 */
	@Override
	public List<MgIdentity> selectMgIdentityList(MgIdentity mgIdentity) {
		return this.getBaseMapper().selectList(mgIdentity);
	}

	/**
	 * 新增身份
	 * 
	 * @param mgIdentity 身份
	 * @return 结果
	 */
	@Override
	public int insertMgIdentity(MgIdentity mgIdentity) {
		insertProperties(mgIdentity);

		redisTemplate.delete(RedisConstant.IDENTITY_TYPE);
		
		redisTemplate.delete("identity:");
		return this.getBaseMapper().insert(mgIdentity);
	}

	/**
	 * 修改身份
	 * 
	 * @param mgIdentity 身份
	 * @return 结果
	 */
	@Override
	public int updateMgIdentity(MgIdentity mgIdentity) {

		redisTemplate.delete(RedisConstant.IDENTITY_TYPE);
		
		redisTemplate.delete("identity:");
		return this.getBaseMapper().update(mgIdentity);
	}

	/**
	 * 批量删除身份
	 * 
	 * @param ids 需要删除的身份主键
	 * @return 结果
	 */
	@Override
	public int deleteMgIdentityByIds(Integer[] ids) {
		
		redisTemplate.delete(RedisConstant.IDENTITY_TYPE);
		
		redisTemplate.delete("identity:");
		
		return this.getBaseMapper().deleteByIds(ids);
	}

	/**
	 * 删除身份信息
	 * 
	 * @param id 身份主键
	 * @return 结果
	 */
	@Override
	public int deleteMgIdentityById(int id) {
		return this.getBaseMapper().deleteById(id);
	}

	@Override
	public List<MgIdentity> getIdentitysByEnabled() {
		return this.listToCache(redisTemplate, RedisConstant.IDENTITY_TYPE, 100);
	}

	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<MgIdentity>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	@Transactional
	public void insertMgIdentitys(List<MgIdentity> mgIdentities) {

		redisTemplate.delete(RedisConstant.IDENTITY_TYPE);
		
		redisTemplate.delete("identity:");
		IBaseMapper<MgIdentity> baseMapper = this.getBaseMapper();
		
		for (MgIdentity o : mgIdentities) {
			
			insertProperties(o);
			baseMapper.insert(o);
		}
	}
}
